package com.td.erhome.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.td.erhome.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/4/19 0019.
 */

public class popAdapterAreaTwo extends RecyclerView.Adapter<popAdapterAreaTwo.PopHolder>{
     private Context mContext;
    private ArrayList<String> mList;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private int layoutPosition;

    public popAdapterAreaTwo(Context context, ArrayList<String> list) {
        mContext = context;
        mList = list;
    }

    public void resetData(ArrayList<String> list,int point){
        mList = list;
        layoutPosition = point;
        notifyDataSetChanged();
    }


    public void setPoint(int point){
        layoutPosition = point;
        notifyDataSetChanged();
    }

    @Override
    public PopHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_pop_item, null);
        popAdapterAreaTwo.PopHolder popHolder = new PopHolder(inflate);
        return  popHolder;
    }

    @Override
    public void onBindViewHolder(final PopHolder holder, int position) {
        holder.mTvItem.setText(mList.get(position));
        holder.itemView.setTag(position);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前点击的位置
                layoutPosition = holder.getLayoutPosition();
                notifyDataSetChanged();
                mOnItemClickListener.onItemClick(layoutPosition);
            }
        });
        //更改状态
        if(position == layoutPosition){
            holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_048d99));
        }else{
            holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_666666));
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(int sn);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public class PopHolder extends RecyclerView.ViewHolder {

        private final TextView mTvItem;

        public PopHolder(View itemView) {
            super(itemView);
            mTvItem = (TextView) itemView.findViewById(R.id.tv_item);

        }
    }
}
